﻿/***************************************************************
 * Name:      new_task_dialog.cpp
 * Purpose:   新建任务对话框
 * Author:    杨卓荦 (yzl@bang5.net)
 * Created:   2010-08-29
 * Copyright: 杨卓荦 (http://www.bang5.net)
 * License:
 **************************************************************/

#include "new_task_dialog.h"
#include "task.h"  // 下载任务类
#include "cloudown_main.h"  // 主框架类
#include <algorithm>
using namespace std;

//(*InternalHeaders(NewTaskDialog)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//(*IdInit(NewTaskDialog)
const long NewTaskDialog::kIdStaticTextURL = wxNewId();
const long NewTaskDialog::kIdTextCtrlURL = wxNewId();
const long NewTaskDialog::kIdTextCtrlFilePath = wxNewId();
const long NewTaskDialog::kIdButtonBrowse = wxNewId();
const long NewTaskDialog::kIdStaticTextDir = wxNewId();
const long NewTaskDialog::kIdButtonStartDownload = wxNewId();
const long NewTaskDialog::kIdTextCtrlThreads = wxNewId();
const long NewTaskDialog::kIdStaticTextThreads = wxNewId();
//*)

BEGIN_EVENT_TABLE(NewTaskDialog, wxDialog)
  //(*EventTable(NewTaskDialog)
  //*)
END_EVENT_TABLE()

NewTaskDialog::NewTaskDialog(wxWindow* parent, wxWindowID id) {
  //(*Initialize(NewTaskDialog)
  Create(parent, wxID_ANY, _("新建下载任务"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
  SetClientSize(wxSize(400,144));
  static_text_url = new wxStaticText(this, kIdStaticTextURL, _("下载地址："), wxPoint(8,8), wxDefaultSize, 0, _T("kIdStaticTextURL"));
  text_ctrl_url = new wxTextCtrl(this, kIdTextCtrlURL, _("http://"), wxPoint(72,8), wxSize(320,22), 0, wxDefaultValidator, _T("kIdTextCtrlURL"));
  text_ctrl_file_path = new wxTextCtrl(this, kIdTextCtrlFilePath, _("D:\\"), wxPoint(72,40), wxSize(240,22), wxTE_READONLY, wxDefaultValidator, _T("kIdTextCtrlFilePath"));
  button_browse = new wxButton(this, kIdButtonBrowse, _("浏览……"), wxPoint(320,40), wxDefaultSize, 0, wxDefaultValidator, _T("kIdButtonBrowse"));
  static_text_dir = new wxStaticText(this, kIdStaticTextDir, _("存储目录："), wxPoint(8,40), wxDefaultSize, 0, _T("kIdStaticTextDir"));
  button_start_download = new wxButton(this, kIdButtonStartDownload, _("下载"), wxPoint(8,104), wxDefaultSize, 0, wxDefaultValidator, _T("kIdButtonStartDownload"));
  text_ctrl_threads = new wxTextCtrl(this, kIdTextCtrlThreads, _("10"), wxPoint(72,72), wxSize(40,22), 0, wxDefaultValidator, _T("kIdTextCtrlThreads"));
  static_text_threads = new wxStaticText(this, kIdStaticTextThreads, _("线程数："), wxPoint(8,72), wxDefaultSize, 0, _T("kIdStaticTextThreads"));
  dirdialog_save = new wxDirDialog(this, _("选择保存目录："), wxEmptyString, wxDD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxDirDialog"));

  Connect(kIdButtonBrowse,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&NewTaskDialog::OnButtonBrowseClick);
  Connect(kIdButtonStartDownload,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&NewTaskDialog::OnButtonStartDownloadClick);
  //*)
}

NewTaskDialog::~NewTaskDialog() {
  //(*Destroy(NewTaskDialog)
  //*)
}


void NewTaskDialog::OnButtonStartDownloadClick(wxCommandEvent& event) {
  // http://download.virtualbox.org/virtualbox/3.2.8/VirtualBox-3.2.8-64453-Win.exe
  // 获得完整的路径名
  wxString full_url = text_ctrl_url->GetValue();
  // 没有文件名，且不是以'/'结尾
  if (2 >= count(full_url.begin(), full_url.end(), '/')) {
    full_url += '/';
  }
  string local_file_name;  // 本地完整路径和文件名
  local_file_name = text_ctrl_file_path->GetValue().mb_str(wxConvFile);
  int threads = 5;  // 线程数
  text_ctrl_threads->GetValue().ToLong((long *)&threads);
  // 创建一个任务，并开始
  Task* task = new Task(full_url, local_file_name, threads);
  ((ClouDownFrame*)GetParent())->GetTaskManager().AddTask(task);
  wxListCtrl& list_ctrl = ((ClouDownFrame*)GetParent())->GetDownloadingCtrl();
  string s;  // 临时字符串变量，用于函数返回值
  task->GetFileName(s);
  int index = list_ctrl.GetItemCount();  // 要插入的位置
  list_ctrl.InsertItem(index, _("下载中"));
  list_ctrl.SetItem(index, 1, wxString(s.c_str(), wxConvFile));
  list_ctrl.SetItem(index, 2, wxString::Format(_("%lld 字节"), task->GetFileSize()));
  list_ctrl.SetItem(index, 3, _("0.00%"));
  list_ctrl.SetItem(index, 4, _("0.00KB/s"));
  list_ctrl.SetItem(index, 5, wxString::Format(_("%d"), task->GetWorkerCount()));
  long long int* item_data = new long long int;
  *item_data = 0;
  list_ctrl.SetItemData(index, (long int)item_data);
  this->EndModal(wxID_OK);
}

void NewTaskDialog::OnButtonBrowseClick(wxCommandEvent& event) {
  if (wxID_OK == dirdialog_save->ShowModal()) { // 选定目录
    text_ctrl_file_path->SetLabel(dirdialog_save->GetPath());
  }
}
